package com.hubiwei.gmall.logger.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

//@Controller
@RestController
@Slf4j
public class LoggerController {
    
    @RequestMapping("/applog")
    public String doLog(@RequestParam("param") String log) {
        // 1. 把数据落盘
    //    saveToDisk(log);

        System.out.println(log);
        // 2. 把数据直接写入到kafka中(ods_log)
      writeToKafka(log);
        
        return "index.html";
    }
    
    @Autowired
    KafkaTemplate<String, String> kafka;
    
    private void writeToKafka(String log) {
        kafka.send("ods_log", log);
    }
    
    private void saveToDisk(String strLog) {
        log.info(strLog);
    }
}
